package com.gongpingjialoan.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.adapter.BrandPopLeftAdapter;
import com.gongpingjialoan.adapter.BrandPopRightAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Administrator on 2016/5/18.
 */
public class BrandPop {
    Context context;

    View contentV;

    PopupWindow pop;

    OnReslutClickListener onReslutClickListener;

    ListView leftlistV, rightListV;

    BrandPopLeftAdapter leftadapter;

    BrandPopRightAdapter rightAdapter;

    View headV;

    View rightHeadV;


    public BrandPop(Context context) {
        this.context = context;
        contentV = LayoutInflater.from(context)
                .inflate(R.layout.pop_brand, null);
        pop = new PopupWindow(contentV, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        // pop.setAnimationStyle(R.style.mystyle);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initView();

    }

    private void initView() {
        contentV.findViewById(R.id.bg).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        pop.dismiss();
                    }
                });

        leftlistV = (ListView) contentV.findViewById(R.id.listview_left);
        headV = LayoutInflater.from(context).inflate(R.layout.head_brand_left_head, null);
        rightHeadV = LayoutInflater.from(context).inflate(R.layout.head_brand_pop_right, null);
        leftlistV.addHeaderView(headV);
        leftadapter = new BrandPopLeftAdapter(context);
        leftlistV.setAdapter(leftadapter);
        leftlistV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0) {
                    if (onReslutClickListener != null) {
                        onReslutClickListener.result("", "", "", "");
                    }
                    pop.dismiss();

                    return;
                }

                JSONObject jo = leftadapter.getItem(position - 1);
                try {
                    rightListV.setVisibility(View.VISIBLE);
                    JSONArray jsa = jo.getJSONArray("models");
                    rightAdapter.setData(jsa);
                    rightAdapter.setParentData(jo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

//                String data = adapter.getItem(position);
//                if (onReslutClickListener != null) {
//                    onReslutClickListener.result(data);
//                }
            }
        });

        rightListV = (ListView) contentV.findViewById(R.id.listview_right);
        rightListV.addHeaderView(rightHeadV);
        rightAdapter = new BrandPopRightAdapter(context);
        rightListV.setAdapter(rightAdapter);
        rightListV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onReslutClickListener != null) {
                    JSONObject parentjo = rightAdapter.getParentData();

                    if (position == 0) {

                        try {
                            onReslutClickListener.result(parentjo.getString("brand_slug_zh"), parentjo.getString("brand_slug"), "", "");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        JSONObject jo = rightAdapter.getItem(position - 1);
                        try {
                            onReslutClickListener.result(parentjo.getString("brand_slug_zh"), parentjo.getString("brand_slug"), jo.getString("model_slug_zh"), jo.getString("model_slug"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                pop.dismiss();
            }
        });
    }


    public void setData(JSONArray jsa, String allcount) {

        String content = "全部  在售(" + allcount + ")辆";
        TextView desT = (TextView) headV.findViewById(R.id.des);

        int index = content.indexOf(allcount);

        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.delear_orange));
        builder.setSpan(redSpan, index, index + allcount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        desT.setText(builder);

        leftadapter.setData(jsa);
    }

    public void show(View v) {
        if (android.os.Build.VERSION.SDK_INT >= 24) {
            int[] a = new int[2];
            v.getLocationInWindow(a);
            pop.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + v.getHeight() + 1);
        } else {
            pop.showAsDropDown(v , 0 ,1);
        }
        if (rightListV != null) {
            rightListV.setVisibility(View.INVISIBLE);
        }
    }

    public OnReslutClickListener getOnReslutClickListener() {
        return onReslutClickListener;
    }

    public void setOnReslutClickListener(
            OnReslutClickListener onReslutClickListener) {
        this.onReslutClickListener = onReslutClickListener;
    }

    public interface OnReslutClickListener {
        void result(String brandName, String brandSlug, String modelname, String modelslug);
    }

}
